#include "semaphore.hpp"
#include <cassert>
#include <iostream>
#include <vector>
#include <thread>
#ifdef _WIN32
    #include <windows.h>
#endif // _WIN32

Semaphore sem(0);
std::atomic<uint32_t> sum(0);

void Wait()
{
    for (uint32_t n = 0; n < 1000000; n++)
    {
        sem.Wait();
        ++sum;
    }
}

void Signal()
{
    for (uint32_t n = 0; n < 1000000; n++)
    {
        sem.Signal();
    }
}

void Test()
{
    std::vector<std::thread> threads;
    for (uint32_t n = 0; n < 10; ++n)
    {
        threads.emplace_back(Signal);
    }
    for (uint32_t n = 0; n < 10; ++n)
    {
        threads.emplace_back(Wait);
    }
    for (auto &it : threads)
    {
        if (it.joinable())
        {
            it.join();
        }
    }
    assert(sum == 10000000);
}

int main()
{
#ifdef _WIN32
    SetConsoleCP(65001);
    SetConsoleOutputCP(65001);
#endif // _WIN32
    Test();
}